package com.platform.web.lisenter;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.manager.AsyncManager;
import com.platform.common.utils.redis.RedisUtil;
import com.platform.common.utils.spring.SpringUtils;
import com.platform.data.domain.EqAlarm;
import com.platform.data.service.IEqAlarmService;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.TimerTask;
import java.util.regex.Pattern;

@Component
@Slf4j
public class AlarmListener {

    @Autowired
    private RedisUtil redisUtil;

    @RabbitListener(queues = "error_queue")
    @RabbitHandler
    public void receiveError(JSONObject object, Channel channel, Message message){
        try{
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            String messageId = message.getMessageProperties().getHeaders().get("spring_returned_message_correlation").toString();
            if(redisUtil.hasKey("message:"+messageId)){
                return;
            }
            redisUtil.set("message:"+messageId, messageId, 1000);
            //执行异步保存数据
            log.info("执行异步保存错误数据");
            AsyncManager.me().execute(AlarmPoolTask.saveAlarm(object.getString("eqId"),object.getString("locationId"), object.getString("eqType"), object.getString("alarmType"),
                    object.getString("alarmMethod"), object.getString("alarmMsg")));
        }catch (IOException e){
            e.printStackTrace();
        }
    }
}

class AlarmPoolTask {
    public static TimerTask saveAlarm(final String eqId,final String locationId, final String eqType, final String alarmType, final String alarmMethod, final String alarmMsg)
    {
        final IEqAlarmService eqAlarmService = SpringUtils.getBean(IEqAlarmService.class);

        final RedisUtil redisUtil = SpringUtils.getBean(RedisUtil.class);

        return new TimerTask() {
            @Override
            public void run() {
                if(eqId!=null && !eqId.equals("")) {
                    Pattern pattern = Pattern.compile("[0-9]*");
                    if(pattern.matcher(eqId).matches()){
                        EqAlarm eqAlarm = new EqAlarm();
                        eqAlarm.setEqId(eqId);
                        eqAlarm.setLocationId(locationId);
                        eqAlarm.setEqType(eqType);
                        eqAlarm.setAlarmType(alarmType);
                        eqAlarm.setAlarmMethod(alarmMethod);
                        eqAlarm.setAlarmMsg(alarmMsg);
                        eqAlarmService.insertEqAlarm(eqAlarm);

                        if(redisUtil.hasKey("error")){
                            redisUtil.incr("error", 1);
                        }
                    }
                }
            }
        };
    }
}
